package com.vm.activity;

import java.util.Timer;
import org.json.JSONObject;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.Response.Listener;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.StringRequest;
import com.vm.commons.StringUtil;
import com.vm.commons.VMConst;
import com.vm.model.UserInfo;
import com.vm.utils.JsonUtils;
import com.vm.utils.MyTimerTask;
import com.vm.utils.MyVolley;
import com.wwtscapp.apk.R;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

public class ForgetPsdActivity extends Activity {
	protected static final String TAG = null;
	private RequestQueue mQueue;
	private RequestQueue rQueue;
	int countDown = 90;
	ImageView imgbtn_search_back;
	EditText et_nickname;
	EditText et_verification_code;
	Button btn_get_verification_code;
	Button btn_next;
	Timer timer;
	String userId;
	boolean isTrue_code = false;
	private MyTimerTask mTimerTask;

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.login_forget_password);
		mQueue = MyVolley.getRequestQueue();
		rQueue = MyVolley.getRequestQueue();
		init();
	}

	public void init() {
		imgbtn_search_back = (ImageView) findViewById(R.id.imgbtn_search_back);
		et_nickname = (EditText) findViewById(R.id.et_nickname);
		et_verification_code = (EditText) findViewById(R.id.et_verification_code);
		btn_get_verification_code = (Button) findViewById(R.id.btn_get_verification_code);
		btn_next = (Button) findViewById(R.id.btn_next);
		imgbtn_search_back.setOnClickListener(btn_backListener);
		btn_get_verification_code.setOnClickListener(getCode_Listener);
		btn_next.setOnClickListener(btn_Listener);
	}

	OnClickListener btn_Listener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			String phone = et_nickname.getText().toString();
			String code = et_verification_code.getText().toString();
			if (phone != null && phone.length() > 0 && code != null
					&& code.length() > 0) {
				String url = VMConst.REQUEST_URL + "checkCode?phone=" + phone
						+ "&code=" + code;
				StringRequest stringRequest = new StringRequest(url,
						new Listener<String>() {
							@Override
							public void onResponse(String arg0) {
								// TODO Auto-generated method stub
								if (arg0.contains("正确")) {
									isTrue_code = true;
									Intent intent = new Intent(
											ForgetPsdActivity.this,
											SetNewPsdActivity.class);
									intent.putExtra("userId", userId);
									startActivity(intent);
									finish();
								} else {
									isTrue_code = false;
									Toast toast3 = Toast.makeText(
											ForgetPsdActivity.this, "验证码错误",
											500);
									toast3.setGravity(Gravity.CENTER, 0, 0);
									toast3.show();
								}

							}
						}, null);
				mQueue.add(stringRequest);
			}
		}

	};
	OnClickListener getCode_Listener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			final String phone = et_nickname.getText().toString();
			if (phone != null && phone.length() > 0) {
				String url = VMConst.REQUEST_URL + "getByPhone?phone=" + phone;
				JsonObjectRequest jsonObjectRequest;
				try {
					jsonObjectRequest = new JsonObjectRequest(url, null,
							new Listener<JSONObject>() {

								@Override
								public void onResponse(JSONObject arg0) {
									// TODO Auto-generated method stub
									try {
										UserInfo userInfo = JsonUtils
												.jsonToBean(arg0,
														UserInfo.class);
										if (userInfo != null) {
											userId = userInfo.getId();
											timer = new Timer(true);
											mTimerTask = new MyTimerTask(
													countDown, handler);
											timer.schedule(mTimerTask, 1000,
													1000);
											String url = VMConst.REQUEST_URL
													+ "getMobileCode?mobilephone="
													+ phone + "&deviceId="
													+ StringUtil.DEVICEID;
											StringRequest stringRequest = new StringRequest(
													url,
													new Listener<String>() {

														@Override
														public void onResponse(
																String arg0) {
															// TODO
															// Auto-generated
															// method stub
															if (arg0.contains("1")) {
																Toast toast3 = Toast
																		.makeText(
																				ForgetPsdActivity.this,
																				"正在发送,请稍后",
																				500);
																toast3.setGravity(
																		Gravity.CENTER,
																		0, 0);
																toast3.show();
															}
														}

													}, null);
											mQueue.add(stringRequest);
											btn_get_verification_code
													.setEnabled(false);
											et_nickname.setEnabled(false);
										}
									} catch (Exception e) {
										// TODO Auto-generated catch block
										e.printStackTrace();
									}
								}

							}, new Response.ErrorListener() {
								@Override
								public void onErrorResponse(VolleyError error) {
									Toast toast = Toast
											.makeText(ForgetPsdActivity.this,
													"手机号输入有误,请重新输入",
													Toast.LENGTH_SHORT);
									toast.show();
								}
							});
					rQueue.add(jsonObjectRequest);
				} catch (Exception e) {
					// TODO Auto-generated catch block
					Log.e(TAG, e.getMessage());
				}
			} else {
				Toast toast = Toast.makeText(ForgetPsdActivity.this,
						"请输入您的注册手机号", Toast.LENGTH_SHORT);
				toast.show();
			}
		}

	};
	OnClickListener btn_backListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			onBackPressed();
		}
	};

	/**
	 * 手机自带返回按钮监听
	 */
	@Override
	public void onBackPressed() {
		super.onBackPressed();

	}

	@SuppressLint("HandlerLeak")
	final Handler handler = new Handler() {
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case 1:
				String remainTime = msg.obj.toString();
				btn_get_verification_code.setText("重新发送" + remainTime);
				break;

			case 2:
				btn_get_verification_code.setText("重新发送");
				btn_get_verification_code.setEnabled(true);
				// task = null;
				// time
				timer.cancel();
				break;
			}

			super.handleMessage(msg);
		}
	};

}
